/*
 * ParserServer.java
 *
 * Created on 16 mai 2007, 16:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package bibliotheque.server.parser;

import bibliotheque.document.DocumentId;
import bibliotheque.server.Server;
import bibliotheque.document.Document;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;


/**
 *
 * @author guillaume
 */
public abstract class ParserServer {
    
    public static Hashtable<DocumentId, Document> GetDocuments(File _FilePath, Server server) throws JDOMException,IOException {
        
        Hashtable<DocumentId, Document> documents = new Hashtable<DocumentId, Document>();
        
        SAXBuilder sxb = new SAXBuilder();
        org.jdom.Document document = sxb.build(_FilePath);
        
        /*########################*/
        /* Demmarage de l'analyse */
        /*########################*/
        
        
        Element racine = document.getRootElement();
        
        XPath xpa = XPath.newInstance("//work");
        List results = xpa.selectNodes(racine);
        Iterator iter_work = results.iterator();
        
        Element noeudCourant = null;
        int number ;
        String summary;
        String content;
        String title;
        
        while (iter_work.hasNext()) {
            
            
            noeudCourant = (Element) iter_work.next();
            
            xpa = XPath.newInstance("./@number");
            number = Integer.parseInt(xpa.valueOf(noeudCourant).toString());
            
            xpa = XPath.newInstance("./summary");
            summary = xpa.valueOf(noeudCourant).toString();
            
            
            xpa = XPath.newInstance("./content");
            content = xpa.valueOf(noeudCourant).toString();
            
            xpa = XPath.newInstance("./title");
            title = xpa.valueOf(noeudCourant).toString();
            
            System.out.println("");
            
            DocumentId id = server.GetNewDocumentId();
            documents.put(id, new Document(id, title, summary, content));
            
            /*
            Enumeration<Integer>  enume;
            enume = documents.keys();
            int i=0;
            int toto=0;
            while (enume.hasMoreElements()) {
                toto = enume.nextElement() ;
                if (toto > i) i=toto;
            }
            System.out.println("plus grand id : "+ i);
             */

            
        }
        
        
        return(documents);
        
            /*
            Enumeration<Document> enume = documents.elements();
             
            while (enume.hasMoreElements()) {
                Document doc = enume.nextElement();
                System.out.println("element summary : " + doc.GetSummary() );
                System.out.println("element content : " + doc.GetContents());
            }
             */
        
        
    }
    
}
